<?php

namespace common\models\searchs;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\AnswerTopic;

/**
 * SearchAnswerTopic represents the model behind the search form about `common\models\AnswerTopic`.
 */
class SearchAnswerTopic extends AnswerTopic
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'grade', 'show', 'status', 'sort'], 'integer'],
            [['topic', 'key'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {   
        $id = $params['id'];
//        echo $id;die();
        $query = AnswerTopic::find()->where(['status'=>$id]);

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'grade' => $this->grade,
            'show' => $this->show,
            'status' => $this->status,
            'sort' => $this->sort,
        ]);

        $query->andFilterWhere(['like', 'topic', $this->topic])
            ->andFilterWhere(['like', 'key', $this->key]);

        return $dataProvider;
    }
}
